package com.minelx.scriptbuilder.script.attribute.function;

import com.minelx.scriptbuilder.script.ScriptContext;
import com.minelx.scriptbuilder.script.attribute.value.IAttribute;
import com.minelx.scriptbuilder.script.operation.IOperation;

import java.util.List;

public class CustomFunction implements Function {

	private final ScriptContext context;

	private final IOperation operation;

	private final List<String> modelParams;

	public CustomFunction(ScriptContext context, List<String> modelParams, IOperation operation) {
		this.context = context;
		this.operation = operation;
		this.modelParams = modelParams;
	}

	public IAttribute execute(Params params) {
		context.pushFunctionFrame(modelParams, params);
		operation.execute();
		IAttribute result = context.functionFrame().returns();
		context.popFunctionFrame();
		return result;
	}
}
